<template>
  <div class="login-container">
    <div class="login-box">
      <img class="logo" src="../assets/logo.png" alt />
      <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <el-form-item prop="username" class="a-login">
          <el-input placeholder="请输入用户名" type="text" v-model="form.username">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" class="a-login">
          <el-input placeholder="请输入密码" type="password" v-model="form.password">
            <!-- <i slot="prefix" class="iconfont icon-lock-fill"></i> -->
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('form')" size="mini">登录</el-button>
          <el-button size="mini" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
/* eslint-disable */
import { login_api } from "@/api/index.js";
export default {
  data() {
    return {
      // 双向绑定表单数据
      form: {
        username: "admin",
        password: "111111"
      },
      // 表单验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 表单重置
    resetForm() {
      this.$refs.form.resetFields();
    },
    // 登录按钮二次校验
    submitForm(form) {
      this.$refs[form].validate(async valid => {
        if (!valid) {
          return;
        } else {
          const res = await login_api(this.form);
          console.log(res);
          if (res.status === 200) {
            this.$message({
              message: "登录成功",
              type: "success"
            });
            window.sessionStorage.setItem("token", res.data.data.token);
            // console.log(window.sessionStorage.getItem('token'));
            //成功后实现跳转
            this.$router.push("/welcome");
          } else {
            this.$message.error("登录失败");
          }
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.login-container {
  position: relative;
  height: 100%;
  background-color: #2a4b6a;
  .login-box {
    padding: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    .logo {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 10px solid #fff;
      background-color: #ccc;
      box-shadow: 0 0 10px #ddd;
    }

    .login-form {
      margin-top: 100px;
      .a-login {
        margin-left: 0 !important;
      }

      .btn {
        text-align: right;
      }
    }
  }
}
</style>
